<template>
  <VueDraggable
    ref="el"
    v-model="volunteerInfos"
    :animation="150"
    ghostClass="ghost"
    class="flex-col gap-2 p-4 w-300px h-300px m-auto bg-gray-500/5 rounded"
    @end="draggableEnd"
  >
    <div
      class="schoolList"
      v-for="(data, index) in volunteerInfos"
      :key="data.id + '' + data.majorsInfo[0].majorCode"
    >
      <div class="flex">
        <div class="schoolList-sequence">
          <el-popover placement="bottom-start" :width="200" trigger="hover">
            <template #reference>
              <el-button style="font-size: 18px" link
                >{{ index + 1 }}&nbsp;
                <el-icon size="12" color="#86909c">
                  <ArrowDownBold />
                </el-icon>
              </el-button>
            </template>
            <div class="curr between">
              <div>第</div>
              <el-input
                v-model="data.inputNum"
                style="width: 52px"
                placeholder="N"
                @input="handleInput(data)"
              ></el-input>
              <div>志愿</div>
              <el-button
                color="#165dff"
                @click="onClickInputChange(data.inputNum, index)"
                >确定</el-button
              >
            </div>
          </el-popover>
        </div>
        <div class="schoolList-Left">
          <div class="df" style="align-items: baseline">
            <div style="font-size: 35px; font-weight: bold">
              {{ data.chance }}
            </div>
            <span>%</span>
          </div>
          <div class="schoolList-Left-size">{{ data.probabilityExplain }}</div>
          <div
            :class="
              data.chance_lv == 1
                ? 'chance1'
                : data.chance_lv == 2
                  ? 'chance2'
                  : 'chance3'
            "
            class="schoolList-Left-number"
          >
            {{ data.chance_lv == 1 ? '冲' : data.chance_lv == 2 ? '稳' : '保' }}
          </div>
        </div>
        <div class="schoolList-major">
          <div class="schoolList-major-name">
            {{ data.majorsInfo[0].professionRealName }}
          </div>
          <div class="schoolList-major-size">
            {{ data.majorsInfo[0].remarkExt }}
          </div>
          <div class="schoolList-major-num margintop">
            代码:
            {{
              data.majorsInfo[0].professionEnrollCode
                ? data.majorsInfo[0].professionEnrollCode
                : '-'
            }}
            <span v-if="!!data.majorsInfo[0].mseLevel"> | </span>
            <span v-if="!!data.majorsInfo[0].mseLevel" class="mseLevel">{{
              data.majorsInfo[0].mseLevel
            }}</span>
          </div>
        </div>
        <div class="schoolList-Center">
          <div class="schoolName">{{ data.collegeRealName }}</div>
          <div class="schoolList-size">
            <p>
              代码: {{ data.collegeEnrollCode }} |
              {{ data.features.join(' / ') }} |
              {{
                data.provinceName +
                ' / ' +
                data.cityName +
                ' / ' +
                data.natureType +
                ' / ' +
                data.categories.join(' / ') +
                ' ' +
                (data.rank ? ' / 排名' + data.rank : '/ 排名 -')
              }}
            </p>
          </div>
        </div>
        <div class="schoolList-people">
          <div class="schoolList-Number" style="font-size: 35px">
            {{ data.planNum }}<span style="font-size: 12px">人</span>
          </div>
          <div class="schoolList-title">
            {{
              (data.majorsInfo[0].learnYear
                ? data.majorsInfo[0].learnYear
                : '-') +
              '年 ￥' +
              (data.majorsInfo[0].cost ? data.majorsInfo[0].cost : '-')
            }}
          </div>
          <div class="schoolList-title">
            {{
              data.majorsInfo[0].chooseSubjectText
                ? data.majorsInfo[0].chooseSubjectText
                : ''
            }}
          </div>
        </div>
        <div class="schoolList-grade">
          <div class="schoolList-grade-left" style="width: 20%">
            <div class="schoolList-grade-size">录取人数</div>
            <div class="schoolList-grade-size">线差</div>
            <div class="schoolList-grade-size">最低分</div>
            <div class="schoolList-grade-size">最低位次</div>
            <div class="schoolList-grade-size schoolList-grade-size-icon">
              <span>等效位差</span>
              <el-tooltip placement="top">
                <template #content>
                  什么是等效位差？
                  <br />
                  历年等效位差根据考生历年的等效位-历年位次的差值得出，
                  <br />
                  换算后的等效位差可更精准的与历年位次进行比较。
                </template>
                <QuestionFilled style="width: 12px; margin-left: 8px" />
              </el-tooltip>
            </div>
          </div>
          <div
            v-for="moreItem in data.majorsInfo[0].moreYear"
            :key="moreItem.enterNum"
            style="width: 27%"
          >
            <div class="item-boxs" v-if="moreItem.hasPlan">
              <div class="schoolList-grade-size-number alitem items">
                {{ moreItem.enterNum }}
              </div>
              <div class="schoolList-grade-size-number alitem items">
                {{ moreItem.lineDiff || '-' }}
              </div>
              <div class="schoolList-grade-size-number alitem items">
                {{ moreItem.minScore }}
                <div
                  v-if="
                    (moreItem.zjText * 1 > 0 && moreItem.minScore * 1 > 0) ||
                    moreItem.zjText == '有征集'
                  "
                  style="background-color: #fbc7c2; color: red"
                >
                  征
                </div>
              </div>
              <div class="schoolList-grade-size-number alitem items">
                {{ moreItem.minRank }}
              </div>
              <div class="schoolList-grade-size-number alitem items">
                <template
                  v-for="(item, index) in getFormatEquivalentBitDifference(
                    moreItem.equivalentBitDifference,
                  )"
                  :key="index"
                >
                  <span :style="{ color: item.color }">
                    {{ item.text }}
                  </span>
                </template>
              </div>
            </div>
            <div v-else class="schoolList-grade-left">
              <span
                class="schoolList-grade-size-number alitem"
                style="height: 100%"
                >未招生</span
              >
            </div>
          </div>
        </div>
        <div v-if="status > 0" class="alitem center schoolList-option">
          <el-icon
            size="20"
            style="margin: 0 20px 0 0; cursor: pointer"
            color="#2990f9"
          >
            <Rank />
          </el-icon>
          <el-popconfirm
            confirm-button-text="删除"
            cancel-button-text="取消"
            icon-color="#626AEF"
            title="确认删除"
            @confirm="onClickDeleteCollege(index)"
          >
            <template #reference>
              <el-icon size="20" color="#2990f9" class="curr">
                <Delete />
              </el-icon>
            </template>
          </el-popconfirm>
        </div>
      </div>
    </div>
  </VueDraggable>
</template>
<script setup>
import { VueDraggable } from 'vue-draggable-plus';
import { useStore } from 'vuex';
import { ElMessage } from 'element-plus';
import { ref, watch, computed } from 'vue';
import _ from 'lodash';
import { safeFormatEquivalentBitDifference } from '@/utils/formatEquivalentBitDifference.js';

const emits = defineEmits(['changeSdInitialLoad']);

const { status } = defineProps({
  status: {
    type: Number,
    default: 0,
  },
});

// 动态获取等效位差的页面结构
const getFormatEquivalentBitDifference = (val) => {
  return safeFormatEquivalentBitDifference(val);
};
// 仓库
const store = useStore();
// 志愿表数据
const storeVolunteerInfos = computed(
  () => store.getters.tianbao.volunteerInfos,
);
const volunteerInfos = ref([]);
// const equivalents = ref([]);
const isInitialLoad = ref(false);
watch(
  () => volunteerInfos.value.map((item) => item.majorsInfo),
  (newValue, oldValue) => {
    if (_.isEqual(newValue, oldValue)) {
      return;
    } else {
      if (oldValue.length) {
        isInitialLoad.value = true;
        emits('changeSdInitialLoad', isInitialLoad.value);
      }
    }
  },
);

watch(
  () => storeVolunteerInfos.value,
  (newVal) => {
    if (newVal) {
      volunteerInfos.value = newVal;
    }
  },
  { immediate: true, deep: true },
);
// 拖拽排序结束
function draggableEnd() {
  store.dispatch('tianbao/setVolunteerInfos', volunteerInfos.value);
}

// 只允许输入数字
const handleInput = (data) => {
  data.inputNum = data.inputNum.replace(/[^\d]/g, '');
};

/**
 * 点击调整志愿表序号
 * @param {Number} inputNum 调整到序号数
 * @param {Number} index 院校下标
 */
function onClickInputChange(inputNum, index) {
  if (inputNum <= 0) {
    ElMessage({
      message: '志愿序号不得小于1',
      type: 'warning',
    });
  } else if (inputNum > volunteerInfos.value.length) {
    ElMessage({
      message: '当前志愿序号不得超过志愿个数',
      type: 'warning',
    });
  } else {
    let moveVolunteer = volunteerInfos.value[index];

    if (inputNum <= index) {
      for (let i = index; i > inputNum - 1; i--) {
        volunteerInfos.value[i] = volunteerInfos.value[i - 1];
      }

      volunteerInfos.value[inputNum - 1] = moveVolunteer;
    } else {
      for (let i = index; i < inputNum; i++) {
        volunteerInfos.value[i] = volunteerInfos.value[i + 1];
      }

      volunteerInfos.value[inputNum - 1] = moveVolunteer;
    }
  }
}

// 点击删除填报的院校
function onClickDeleteCollege(index) {
  if (volunteerInfos.value.length == 1) {
    ElMessage({
      message: '请至少保留一个志愿！！！',
      type: 'warning',
    });
    return;
  }

  volunteerInfos.value.splice(index, 1);
  store.dispatch('tianbao/setVolunteerInfos', volunteerInfos.value);
}
</script>
<style lang="scss" scoped>
.item-boxs {
  display: flex;
  height: 100%;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;

  .items {
    height: 25%;
    width: 100%;
    border-bottom: 1px solid #f2f3f5;
    border-right: 1px solid #f2f3f5;
    padding: 10px 4px 10px 10px;
    font-size: 12px;
    font-weight: 400;
    line-height: 12px;
  }
}

.schoolList {
  display: flex;
  flex-direction: column;
  border-right: none;
  background: #fff;
  position: relative;
  border-bottom: 1px solid #e5e6eb;

  .schoolList-look-button {
    color: #165dff;
    width: 90px;
    height: 40px;
    background-color: #fff;
    border: 1px solid #165dff;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
  }

  .schoolList-grade {
    display: flex;
    width: 40%;

    .schoolList-grade-left {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      height: 100%;

      .schoolList-grade-size {
        height: 25%;
        padding: 10px 5px;
        text-align: left;
        font-size: 12px;
        font-weight: 400;
        line-height: 12px;
        border-bottom: 1px solid #f2f3f5;
        border-right: 1px solid #f2f3f5;
      }

      .schoolList-grade-size-number {
        padding: 10px 4px 10px 0;
        text-align: left;
        font-size: 12px;
        font-weight: 400;
        line-height: 12px;
        border-bottom: 1px solid #f2f3f5;
        border-right: 1px solid #f2f3f5;
        padding-left: 10px;
      }

      .schoolList-grade-size-icon {
        align-items: center;
        display: flex;
        flex-direction: row;
      }
    }
  }

  .schoolList-option {
    width: 10%;
  }

  .schoolList-people {
    display: flex;
    width: 9.2%;
    flex-direction: column;
    text-align: center;
    padding: 18px 0;
    border-right: 1px solid #e5e6eb;

    .schoolList-Number {
      text-align: center;
      font-size: 20px;
      font-weight: 400;
      color: #1d2129;
      line-height: 24px;
    }

    .schoolList-title {
      font-size: 12px;
      font-weight: 400;
      color: #86909c;
      line-height: 1.5;
    }
  }

  .schoolList-major {
    width: 13%;
    padding: 14px;
    border-right: 1px solid #e5e6eb;

    .schoolList-major-name {
      font-size: 14px;
      font-weight: 500;
      color: #1d2129;
      line-height: 14px;
    }

    .schoolList-major-size {
      margin-top: 8px;
      font-size: 14px;
      font-weight: 400;
      color: #4e5969;
      line-height: 24px;
    }

    .schoolList-major-num {
      font-size: 12px;
      font-weight: 400;
      color: #86909c;
      line-height: 18px;

      .mseLevel {
        background-color: #fbe6e8;
        padding: 2px 4px;
        font-size: 10px;
        color: #f54f4a;
      }
    }
  }

  .schoolList-Center {
    width: 17%;
    margin-bottom: 10px;

    .schoolName {
      font-size: 18px;
      font-weight: 500;
      color: #1d2129;
      line-height: 27px;
      margin-left: 20px;
      margin-top: 20px;
    }

    .schoolName-size {
      font-size: 14px;
      font-weight: 400;
      color: #1d212c;
      line-height: 27px;
      margin-left: 20px;
      margin-top: 20px;
    }

    .schoolList-major-size {
      margin-top: 8px;
      font-size: 14px;
      font-weight: 400;
      color: #4e5969;
      line-height: 24px;
    }

    .schoolList-size {
      font-size: 12px;
      font-weight: 400;
      color: #86909c;
      line-height: 24px;
      display: flex;
      margin-left: 20px;
      align-items: center;
    }

    .schoolList-bottom {
      display: flex;
      margin-left: 20px;
      margin-top: 10px;
      margin-bottom: 10px;

      .schoolList-bottom-size {
        background: #fff;
        border-radius: 2px 2px 2px 2px;
        border: 1px solid #bcc3cc;
        font-size: 12px;
        font-weight: 400;
        color: #86909c;
        padding: 0 4px;
        margin: 0 4px;
      }
    }
  }

  .schoolList-sequence {
    width: 7%px;
    padding-top: 18px;
    padding-left: 10px;
  }

  .schoolList-Left {
    width: 7%;
    padding: 18px 0;
    padding-left: 20px;
    border-right: 1px solid #f2f3f5;

    .schoolList-Left-size {
      text-align: left;
      font-size: 12px;
      font-family:
        PingFang SC-Regular,
        PingFang SC;
      font-weight: 400;
      color: #86909c;
      line-height: 12px;
    }

    .schoolList-Left-number {
      width: 20px;
      height: 20px;
      // background-color: #F53F3F;
      text-align: center;
      line-height: 20px;
      border-radius: 4px;
      color: #fff;
    }
  }
}

.chance1 {
  background-color: #f53f3f;
  margin-top: 10px;
}

.chance2 {
  background-color: #ff8c33;
}

.chance3 {
  background-color: #00bf10;
}
</style>
